// base64格式的图片
const dataURLtoBlob = (dataurl) => {
  let arr = dataurl.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = window.atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

// 将blob对象转化成文件并导出到本地
const saveAs = (blob, filename) => {
  let link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = filename
  link.click()
}

// 将base64格式的图片下载到本地
const exportImage = (base64Str, filename) => {
  let blob = dataURLtoBlob(base64Str)
  saveAs(blob, filename)
}

export { exportImage }
